package com.autumn.juc.threadpool;

import java.util.concurrent.*;

/**
 *
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService service1 = Executors.newCachedThreadPool();//快
        ExecutorService service2 = Executors.newFixedThreadPool(100);//慢
        ExecutorService service3 = Executors.newSingleThreadExecutor();//最慢

        //自定义线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20,
                0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10));

        for (int i = 0; i <= 100; i++) {
            executor.execute(new MyTask(i));
        }
    }
}

/**
 * 项目
 */
class MyTask implements Runnable {
    int i = 0;

    public MyTask(int i) {
        this.i = i;
    }

    @Override
    public void run() {
        System.out.println("程序员做第" + i + "个项目");
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
